<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use App\Models\DevItem;
use App\Models\DevApp;

class DevAppController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '应用程序开发';
	protected $item_name;


    /**
     * Create interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function create(Content $content)
    {
        $item_name = request()->get('item');
		if(empty($item_name)) return '未指定项目!';
		
		$item = DevItem::where('id',$item_name)->orWhere('name',$item_name)->firstOrFail();		
		
		$this->item_name = $item->name;
		
		return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DevApp);

        $grid->column('id', __('ID'))->sortable();
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(DevApp::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
		$form = new Form(new DevApp);

        $form->display('id', __('ID'));
		$form->text('name', __('名称'))
			->creationRules('required|unique:items,name|regex:/^[a-z][a-z_]+$/',['regex' => '名称必须是英文开头且只包含小写字母、下划线'])
			->updateRules('required|unique:items,name,{{name}}|regex:/^[a-z][a-z_]+$/',['regex' => '名称必须是英文开头且只包含小写字母、下划线']);
		$form->text('title', __('标题'))->rules('required');
		$form->text('item_name', __('所属项目'))->rules('required')->default($this->item_name);
        $form->display('created_at', __('Created At'));
        $form->display('updated_at', __('Updated At'));

        return $form;
    }
}
